package wetalk.software.bupt.com.wetalk.view.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jyt.message.Message;
import com.jyt.util.MySerializable;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import wetalk.software.bupt.com.wetalk.GloableParams;
import wetalk.software.bupt.com.wetalk.R;
import wetalk.software.bupt.com.wetalk.application.MyApplication;
import wetalk.software.bupt.com.wetalk.application.UserManager;
import wetalk.software.bupt.com.wetalk.communicate.AndroidClientService;
import wetalk.software.bupt.com.wetalk.model.po.User;
import wetalk.software.bupt.com.wetalk.util.HttpUtil;
import wetalk.software.bupt.com.wetalk.util.MyAsyncTask;


public class VerifyActivity extends AppCompatActivity {
    private static final String Tag="VerifyActivity";
    private AndroidClientService.SendBinder sendBinder;
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            sendBinder= (AndroidClientService.SendBinder) iBinder;
        }
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify);
        initTitle();
        Intent bindIntent = new Intent(this, AndroidClientService.class);
        this.bindService(bindIntent, connection, BIND_AUTO_CREATE);

        Button bt=(Button)findViewById(R.id.bt_verify);
        final EditText etVerify=(EditText)findViewById(R.id.et_verify);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String verifyMessage = etVerify.getText().toString();
                sendAddFriend(verifyMessage);
                Toast.makeText(VerifyActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        });

    }
    private void initTitle() {
        RelativeLayout rvRight = (RelativeLayout) findViewById(R.id.rv_img_right);
        rvRight.setVisibility(View.GONE);
        TextView txt = (TextView) findViewById(R.id.txt_title);
        txt.setText("验证申请");
        RelativeLayout rvLeft = (RelativeLayout) findViewById(R.id.rv_img_back);
        rvLeft.setVisibility(View.VISIBLE);
        rvLeft.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void sendAddFriend(String message){
        final String userId= UserManager.getInstance(VerifyActivity.this).getCurrentUser().getUserID();
        final String targetId = "sys_friend";
        User user=(User)getIntent().getSerializableExtra("user");
        final JSONObject json=new JSONObject();
        try {
            json.put("id",user.getUserID());
            json.put("name",UserManager.getInstance(this).getCurrentUser().getUserName());
            json.put("avatar",UserManager.getInstance(this).getCurrentUser().getAvatar());
            json.put("message",message);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {

                byte[] bs;
                bs = MySerializable.object_bytes(json.toString());

                Message msg = new Message(userId,targetId,"addFri",bs);

                sendBinder.sendMessage(msg);
            }
        });
        thread.start();
        Log.d(Tag,"sendAddFriend-->"+json.toString());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unbindService(connection);
    }
}


